<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="所属用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="所属用户id"></el-input>
    </el-form-item>
    <el-form-item label="设备名称" prop="devName">
      <el-input v-model="dataForm.devName" placeholder="设备名称"></el-input>
    </el-form-item>
    <el-form-item label="设备编号" prop="devSn">
      <el-input v-model="dataForm.devSn" placeholder="设备编号"></el-input>
    </el-form-item>
    <el-form-item label="终端名称" prop="terminalName">
      <el-input v-model="dataForm.terminalName" placeholder="终端名称"></el-input>
    </el-form-item>
    <el-form-item label="终端key" prop="terminalKey">
      <el-input v-model="dataForm.terminalKey" placeholder="终端key"></el-input>
    </el-form-item>
    <el-form-item label="设备地址" prop="devAddress">
      <el-input v-model="dataForm.devAddress" placeholder="设备地址"></el-input>
    </el-form-item>
    <el-form-item label="采集频率" prop="collectCron">
      <el-input v-model="dataForm.collectCron" placeholder="采集频率"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          userId: '',
          devName: '',
          devSn: '',
          terminalName: '',
          terminalKey: '',
          devAddress: '',
          collectCron: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '所属用户id不能为空', trigger: 'blur' }
          ],
          devName: [
            { required: true, message: '设备名称不能为空', trigger: 'blur' }
          ],
          devSn: [
            { required: true, message: '设备编号不能为空', trigger: 'blur' }
          ],
          terminalName: [
            { required: true, message: '终端名称不能为空', trigger: 'blur' }
          ],
          terminalKey: [
            { required: true, message: '终端key不能为空', trigger: 'blur' }
          ],
          devAddress: [
            { required: true, message: '设备地址不能为空', trigger: 'blur' }
          ],
          collectCron: [
            { required: true, message: '采集频率不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/zhiyao/zydevice/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.data.zyDevice
                // this.dataForm.userId = data.zydevice.userId
                // this.dataForm.devName = data.zydevice.devName
                // this.dataForm.devSn = data.zydevice.devSn
                // this.dataForm.terminalName = data.zydevice.terminalName
                // this.dataForm.terminalKey = data.zydevice.terminalKey
                // this.dataForm.devAddress = data.zydevice.devAddress
                // this.dataForm.collectCron = data.zydevice.collectCron
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/zhiyao/zydevice/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                zyDevice: this.dataForm
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  // duration: 1500,
                  duration: 500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
